package copy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileCopy extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JButton[] bt=new JButton[2];
	private JButton copy;
	private JTextField[] tf=new JTextField[2];
	private JProgressBar pb;
	private File srcFile;
	public FileCopy() {
		setSize(500, 500);
		setLocation(700,200);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);
		
		String[] str={"源文件","目标文件夹"};
		int x=50,y=50;
		for(int i=0;i<str.length;i++){
			JLabel label=new JLabel(str[i]);
			label.setBounds(x, y, 70, 25);
			add(label);
			tf[i]=new JTextField();
			tf[i].setBounds(x+label.getWidth(), y, 200, 25);
			add(tf[i]);
			bt[i]=new JButton("···");
			bt[i].setBounds(x+label.getWidth()+tf[i].getWidth(), y, 50, 25);
			bt[i].addActionListener(this);
			add(bt[i]);
			y+=50;
		}
		
		pb=new JProgressBar(0, 100);//创建进度条组件
		pb.setBounds(80, 220, 300, 25);
		pb.setStringPainted(true);	//为真则值进度条上显示百分比
		add(pb);
		
		copy=new JButton("复制");
		copy.setBounds(130, 330, 200, 25);
		copy.addActionListener(this);
		add(copy);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				new FileCopy();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		Object o=e.getSource();//获取点击的按钮
		if(o==bt[0]){
			JFileChooser fc=new JFileChooser();//文件选择组件
			int f=fc.showOpenDialog(this);//打开文件
			if(f==JFileChooser.APPROVE_OPTION){//JJFileChooser组件使用方法
				srcFile=fc.getSelectedFile();
				tf[0].setText(srcFile.getPath());
			}
		}
		else if(o==bt[1]){
			JFileChooser fc=new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只能选择文件夹
			int f=fc.showSaveDialog(this);//显示选择文件夹
			if(f==JFileChooser.APPROVE_OPTION){
				tf[1].setText(fc.getSelectedFile().getPath());
			}
		}
		else if(o==copy){
			File desFile;
			String path=tf[1].getText()+"\\"+srcFile.getName();//文件被复制到的路径及文件名
			desFile=new File(path);//创建文件
			new Thread(new Runnable() {//创建线程并创建类部类来实现线程run方法
				
				@Override
				public void run() {
					try {
						BufferedInputStream in=new BufferedInputStream(
								new FileInputStream(srcFile));
						BufferedOutputStream out=new BufferedOutputStream(
								new FileOutputStream(desFile));	//实现文件复制
						long size=srcFile.length();//获取文件的大小
						int a,percent,c=0;
						byte[] t=new byte[500];//每次读取500字节
						while((a=in.read(t))!=-1){//读取内容，字节读取
							out.write(a);//写入读取到的内容
							c+=a;//计算输入速度
							percent=(int) ((1.0*c/size)*100);//计算百分比
							pb.setValue(percent);//设置进度
						}						
						in.close();out.flush();out.close();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}).start();//线程创建结束并启动线程
		}
	}

}

